package com.atung.service.impl;

import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.atung.dao.BusCarAllocationDAO;
import com.atung.dao.BusCarRideDAO;
import com.atung.dao.BusOrderCarDAO;
import com.atung.model.BusCarRide;
import com.atung.model.BusOrderCar;
import com.atung.model.BusOrderCarDTO;
import com.atung.model.CarMessageDTO;
import com.atung.model.CarRidePeopleDTO;
import com.atung.model.CompanionDTO;
import com.atung.request.RequestIdentityVerification;
import com.atung.response.ResponeRefundDetails;
import com.atung.response.ResponeRefundDetailsIng;
import com.atung.response.ResponeVerificationIdentity;
import com.atung.response.ResponseBusOrderCarList;
import com.atung.response.ResponseCompanion;
import com.atung.response.ResponseOrderDetails;
import com.atung.service.BusOrderCarService;

import cn.hutool.core.bean.BeanUtil;

@Service
public class BusOrderCarServiceImpl implements BusOrderCarService{

	@Autowired
	private BusCarAllocationDAO busCarAllocationDAO;
	@Autowired
	private BusOrderCarDAO busOrderCarDAO;
	@Autowired
	private BusCarRideDAO busCarRideDAO;
	/**
	 * 订单列表
	 * @param openId
	 * @param orderState
	 * @return
	 */
	public List<ResponseBusOrderCarList>getOrderList(String openId, Integer orderState){
		
		
		return busOrderCarDAO.getOrderList(openId, orderState);
	}
	
	/**
	 * 订单详情
	 * @param orderId
	 * @return
	 */
	public ResponseOrderDetails getOrderDetails(Long orderId){
		ResponseOrderDetails responseOrderDetails=new ResponseOrderDetails();
		BusOrderCarDTO busOrderCarDTO=busOrderCarDAO.getOrderDetails(orderId);
		if(Objects.isNull(busOrderCarDTO)){
			return responseOrderDetails;
		}
		List<BusCarRide> BusCarRidelist=busCarRideDAO.selectBusCarRideByOrderId(orderId);
		List<CompanionDTO> companionlist=new ArrayList<CompanionDTO>();
		for(BusCarRide busCarRide:BusCarRidelist){
			CompanionDTO companionDTO=new CompanionDTO();
			companionDTO.setCompanionName(busCarRide.getRideName());
			companionDTO.setCompanionPhone(busCarRide.getRidePhone());
			companionDTO.setSurname(busCarRide.getRideName().subSequence(0,1).toString());
			companionlist.add(companionDTO);
			
		}
		responseOrderDetails.setCompanionlist(companionlist);;
		BeanUtil.copyProperties(busOrderCarDTO, responseOrderDetails);
		if(Objects.nonNull(busOrderCarDTO.getAllocationId())){
			CarMessageDTO carMessageDTO=busCarAllocationDAO.selectAdmCarMessage(busOrderCarDTO.getAllocationId());
			if(Objects.nonNull(carMessageDTO)){
				if(responseOrderDetails.getOrderState()==300){
					busOrderCarDAO.updateBusOrderCarOrderStateToTrave(orderId);
				}
			}
			responseOrderDetails.setCarMessageDTO(carMessageDTO);			
		}
		
		return responseOrderDetails;
	}

//	@Override
//	@Transactional
//	public void riding(Long orderId) throws Exception {
//		// TODO 自动生成的方法存根
//		
//		BusOrderCar busOrderCar=busOrderCarDAO.selectBusOrderCarById(orderId);
//		if(Objects.isNull(busOrderCar)){
//			throw new Exception("订单编号不存在");
//		}
//		if(busOrderCar.getOrderState()!=301){
//			throw new Exception("该订单不能操作上车行为"+busOrderCar.getOrderState());
//		}
//		
//		busOrderCarDAO.updateBusOrderCarOrderStateIsInTravel(orderId);
//		busCarRideDAO.updateBusCarRideisRideByorderId(orderId);
//
//	}

	/**
	 * 验证身份
	 */
	@Override
	@Transactional
	public ResponeVerificationIdentity identityVerification(RequestIdentityVerification request) throws Exception {
		// TODO 自动生成的方法存根
		ResponeVerificationIdentity responeVerificationIdentity=new ResponeVerificationIdentity();
		BusCarRide busCarRide=busOrderCarDAO.selectBusCarRideByAllocationIdAndRideCode(request.getAllocationId(),request.getCode());
		if(Objects.isNull(busCarRide)){
			throw new Exception();
		}
		BusOrderCarDTO busOrderCarDTO=busOrderCarDAO.getOrderDetails(busCarRide.getOrderId());
		responeVerificationIdentity.setDepartlName(busOrderCarDTO.getDepartlName());
		responeVerificationIdentity.setDestination(busOrderCarDTO.getDestination());
		busOrderCarDAO.updateBusCarRideByisRide(busCarRide.getId(),LocalDateTime.now());
		 List<CarRidePeopleDTO> carRidePeopleDTOS=new ArrayList<>();
		 List<BusCarRide> busCarRideList=busOrderCarDAO.selectBusCarRideByOrderId(busCarRide.getOrderId());
		 for(BusCarRide busCarRides:busCarRideList){
			 	CarRidePeopleDTO carRidePeopleDTO=new CarRidePeopleDTO();
				BeanUtil.copyProperties(busCarRides, carRidePeopleDTO);
				carRidePeopleDTO.setSurname(String.valueOf(busCarRides.getRideName().charAt(0)));
				carRidePeopleDTOS.add(carRidePeopleDTO);
			}
		 responeVerificationIdentity.setCompanion(carRidePeopleDTOS);
		return responeVerificationIdentity;
	}

	@Override
	public ResponeRefundDetails refundDetails(Long orderId) {
		// TODO 自动生成的方法存根
		ResponeRefundDetails refundDetails=busOrderCarDAO.refundDetails(orderId);
		if(Objects.isNull(refundDetails)){
			return refundDetails;
		}else{
			System.out.println(refundDetails.getRefundPrincipal().compareTo(BigDecimal.ZERO)>0.1);
			if(refundDetails.getOrderState()>300&&refundDetails.getRefundPrincipal().compareTo(BigDecimal.ZERO)>0.1){
				BigDecimal refundPrincipal=refundDetails.getRefundPrincipal();
				BigDecimal penalSum=refundPrincipal.divide(new BigDecimal("2"),BigDecimal.ROUND_HALF_UP);
				refundDetails.setPenalSum(penalSum);
				refundDetails.setReimbursement(refundPrincipal.subtract(penalSum));
				return refundDetails;
			}
			refundDetails.setPenalSum(new BigDecimal("0.00"));
			refundDetails.setReimbursement(refundDetails.getRefundPrincipal());
			
		}
		return refundDetails;
	}

	@Override
	public ResponeRefundDetailsIng refundDetailsIng(Long orderId) {
		// TODO 自动生成的方法存根
		ResponeRefundDetailsIng ResponeRefundDetailsIng=new ResponeRefundDetailsIng();
		ResponeRefundDetailsIng ResponeRefundDetails=busOrderCarDAO.refundDetailsIng(orderId);
		if(Objects.isNull(ResponeRefundDetails)){
			return ResponeRefundDetailsIng;
		}
		BeanUtil.copyProperties(ResponeRefundDetails, ResponeRefundDetailsIng);
		return ResponeRefundDetailsIng;
	}
}
